﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.Text;
using System.IO;

public partial class Manage_Service_importquestion : ManagePageBase
{

    protected void Page_Load(object sender, EventArgs ea)
    {
        int cid = int.Parse(Request.Form["cid"].Trim());
        string qtype = Request.Form["qtype"].Trim();
        string qdiff = Request.Form["qdiff"].Trim();
        string qknow = Request.Form["qknow"].Trim();
        string filename = Request.Form["filename"].Trim();

        string errList = string.Empty;//保存发生错误题目
        string exitsList = string.Empty;

        if (!IsPostBack)
        {
            //读取EXCEL表获取数据
            string path = Server.MapPath("~/upload/" + filename);
            DataTable dt = ExcelHelper.getFridtTable(path);
            
            foreach (DataRow dr in dt.Rows)
            {
                
                //增加试题
                string QType = qtype;
                string QDifficulty = qdiff;
                string QKnowledge = qknow;

                string Qcontent = Server.HtmlEncode(dr["题目"].ToString().Trim());
                string a = Server.HtmlEncode(dr["选项A"].ToString().Trim());
                string b = Server.HtmlEncode(dr["选项B"].ToString().Trim());
                string c = Server.HtmlEncode(dr["选项C"].ToString().Trim());
                string d = Server.HtmlEncode(dr["选项D"].ToString().Trim());
                string e = Server.HtmlEncode(dr["选项E"].ToString().Trim());

                string enQcontent = Server.HtmlEncode(dr["EN"].ToString().Trim());
                string ena = Server.HtmlEncode(dr["ENA"].ToString().Trim());
                string enb = Server.HtmlEncode(dr["ENB"].ToString().Trim());
                string enc = Server.HtmlEncode(dr["ENC"].ToString().Trim());
                string end = Server.HtmlEncode(dr["END"].ToString().Trim());
                string ene = Server.HtmlEncode(dr["ENE"].ToString().Trim());

                string QAnswer = dr["答案"].ToString().Trim();

                Qcontent += "<br class='qcontentbr'>" + enQcontent;

                //检索试题
                if (!ExitsQuestion(Qcontent, QType, cid))
                {
                    try
                    {
                        int questionid = new Exam.Core.DAL.Question().Add(Qcontent, QAnswer, QType, QDifficulty, QKnowledge, cid, "0");
                        if (QType == "1" || QType == "2" || QType == "3")
                        {
                            string[] opts = new string[] { a + "<br class='optbr'>" + ena, b + "<br class='optbr'>" + enb, c + "<br class='optbr'>" + enc, d + "<br class='optbr'>" + end, e + "<br class='optbr'>" + ene };
                            int result = new Exam.Core.DAL.Question().AddOpts(questionid, opts);
                        }
                    }
                    catch
                    {
                        errList += Qcontent + "\r\n";
                    }
                }
                else
                {
                    exitsList += Qcontent + "\r\n";
                }

            }
            string logname = DateTime.Now.ToString("yyyyMMddhhmmssffffff") + ".txt";
            string _path = Server.MapPath("~/logs/" + logname);
            Tools.WriteTxt("以下试题由于发生错误导入失败\r\n" + errList + "\r\n-----\r\n以下试题由于已经在数据库存在未导入[该课程该题型下存在该题目]" + exitsList, _path);
            File.Delete(path);
            Response.Write(logname);
            Response.End();

        }


    }

    private bool ExitsQuestion(string Qcontent,string QType,int cid)
    {
        bool result = true;
        string SQL = "SELECT COUNT(*) FROM Question WHERE QuestionContent=@QuestionContent AND QuestionTypeId=@QuestionTypeId AND CourseId=@CourseId";
        OleDbParameter[] parameters = {
					new OleDbParameter("@QuestionContent", OleDbType.LongVarChar),
                    new OleDbParameter("@QuestionTypeId", OleDbType.Integer,4),
                    new OleDbParameter("@CourseId", OleDbType.Integer,4)
                                      
                                      };
        parameters[0].Value = Qcontent;
        parameters[1].Value = int.Parse(QType);
        parameters[2].Value = cid;

        object o = Exam.Core.DAL.DataProvider.GetSingle(SQL,parameters);
        if (null != o)
        {
            if (o.ToString().Trim() == "0")
            {//不存在 返回false
                result = false;
            }
        }
        return result;
    }


}